﻿using System;
using System.Threading;
using System.Web.Mvc;
using Smarly.Bussines.Extentions;
using Smarly.Bussines.Model.Payment.YandexMoney;
using Smarly.Core;
using Smarly.Web.Areas.Profile.Helpers;
using Smarly.Web.Controllers.Base;

namespace Smarly.Web.Controllers
{
	public class YandexController : BaseController
	{
		public ActionResult Index(Guid bookId, string value)
		{
			//var hashtag = "pro-jquery-data-and-ajax";

			//((SmarlyIdentity)User.Identity).UserId = new Guid("9f3bf65d-2828-41eb-9e2a-f0b2343efbc1");

			var item = new PurchaseItem {BookId = bookId, Price = value}.Post(User.Identity as SmarlyIdentity);
			//string urlReferrer = Request.UrlReferrer != null ? Request.UrlReferrer.PathAndQuery : "/";
			return Redirect(Engine.GetTokenRequestURL(item.PurchaseId, item.PurchaseHashCode, item.Price));
		}

		public ActionResult Payment(Authecation model, long purchaseId, string purchaseHashCode)
		{
			string errorMessage = null;

			PaymentProcess processPayment = null;

			model.Put(purchaseId, purchaseHashCode);

			if (model.IsValid)
			{
				AuthecationToken auth = Engine.GetAccessTokenFromTemporaryToken(model.code, purchaseId, purchaseHashCode);
				auth.Put(purchaseId);
				if (auth.IsValid)
				{
					var purchase = new PurchaseItem {PurchaseId = purchaseId}.Get();

					string purchaseCost = purchase.Price;
					string clientComment = string.Format("smarly.net: '" + model.BookName + "'");

					PaymentRequest requestPayment = Engine.RequestPayment(auth.access_token, purchaseId, purchaseCost, clientComment);
					requestPayment.Put(purchaseId);
					if (requestPayment.IsValid)
					{
						processPayment = Engine.ProcessPayment(auth.access_token, requestPayment.request_id);
						processPayment.Put(purchaseId);

						while (!processPayment.IsValid.HasValue)
						{
							Thread.Sleep(10 * 1000);
							processPayment = Engine.ProcessPayment(auth.access_token, requestPayment.request_id);
							processPayment.Put(purchaseId);
						}

						if (!processPayment.IsValid.Value)
						{
							errorMessage = processPayment.ErrorDescription;
							Message(processPayment.ErrorCode, processPayment.ErrorCode.ToMessageValue());
						}
					}
					else
					{
						errorMessage = requestPayment.ErrorDescription;
						Message(requestPayment.ErrorCode, requestPayment.ErrorCode.ToMessageValue());
					}
				}
				else
				{
					errorMessage = auth.ErrorDescription;
					Message(auth.ErrorCode, auth.ErrorCode.ToMessageValue());
				}
			}
			else
			{
				errorMessage = model.ErrorDescription;
				Message(model.ErrorCode, model.ErrorCode.ToMessageValue());
			}

			if (string.IsNullOrWhiteSpace(errorMessage) && processPayment != null)
			{
/*
				var title = string.Empty; // processPayment.Title;
				var email = string.Empty; // model.UserEmail;
				var messageValue = string.Empty; // (!model.IsSubscriber).ToMessageValue();
*/
				//Message(Bussines.Message.PaymentSuccess, model.UserEmail/*, title, email, messageValue*/);
			}

			return Redirect(model.BookHash != null ? Url.EbookDownload(model.BookHash) : "/");
		}


	}

}
